package com.example.myapplicationktkt2.canvas

import android.content.Context
import android.graphics.*
import android.os.Bundle
import android.util.AttributeSet
import android.view.View
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import com.example.myapplicationktkt2.R

/**
 * 1. 主要功能特性
 * 五种不同类型的Canvas样式展示：
 * 基本图形绘制（线条、矩形、圆形、椭圆、圆角矩形）
 * Paint样式（STROKE、FILL、FILL_AND_STROKE）
 * 渐变效果（线性渐变、径向渐变、扫描渐变）
 * 路径绘制（基本路径、弧形、贝塞尔曲线）
 * 文本样式（普通、加粗、斜体、下划线、删除线）
 * 交互式切换：
 * 点击"切换样式"按钮可以在五种不同样式间切换
 * 每种样式展示了不同的Canvas绘图技术和效果
 * 2. 技术实现细节
 * 自定义View：
 * CanvasDemoView类继承自View，重写了onDraw方法来实现自定义绘制
 * 使用Paint类控制绘制样式和颜色
 * 使用Path类创建复杂路径
 * 绘图技术：
 * 基本形状绘制：drawLine、drawRect、drawCircle、drawOval、drawRoundRect
 * 渐变效果：LinearGradient、RadialGradient、SweepGradient
 * 路径绘制：Path类的moveTo、lineTo、quadTo、cubicTo等方法
 * 文本样式：通过Paint类的属性如isFakeBoldText、textSkewX等控制
 * 用户界面：
 * 创建了独立的Activity和布局文件
 * 添加了切换按钮来在不同样式间切换
 * 通过主界面可以访问这个Demo
 * 3. 使用方法
 * 运行应用后，在主界面点击"Canvas 样式 Demo"按钮进入Demo
 * 查看当前展示的Canvas样式效果
 * 点击"切换样式"按钮可以在五种不同样式间切换：
 * 基本图形样式
 * Paint样式
 * 渐变效果样式
 * 路径绘制样式
 * 文本样式
 *
 */
class CanvasStyleDemoActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_canvas_style_demo)

        val resetButton = findViewById<Button>(R.id.reset_button)
        val canvasView = findViewById<CanvasDemoView>(R.id.canvas_view)

        resetButton.setOnClickListener {
            canvasView.reset()
        }
    }
}

class CanvasDemoView : View {
    private val paint = Paint()
    private val path = Path()
    private var styleIndex = 0

    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        
        // 清除画布
        canvas.drawColor(Color.WHITE)

        when (styleIndex) {
            0 -> drawBasicShapes(canvas)
            1 -> drawPaintStyles(canvas)
            2 -> drawGradients(canvas)
            3 -> drawPaths(canvas)
            4 -> drawTextStyles(canvas)
        }
    }

    private fun drawBasicShapes(canvas: Canvas) {
        // 绘制线条
        paint.color = Color.BLACK
        paint.strokeWidth = 5f
        paint.style = Paint.Style.STROKE
        canvas.drawLine(50f, 50f, 200f, 50f, paint)

        // 绘制矩形
        paint.color = Color.RED
        paint.style = Paint.Style.FILL
        canvas.drawRect(50f, 100f, 200f, 200f, paint)

        // 绘制圆形
        paint.color = Color.GREEN
        canvas.drawCircle(300f, 150f, 50f, paint)

        // 绘制椭圆
        paint.color = Color.BLUE
        canvas.drawOval(50f, 250f, 200f, 350f, paint)

        // 绘制圆角矩形
        paint.color = Color.YELLOW
        canvas.drawRoundRect(250f, 250f, 400f, 350f, 20f, 20f, paint)
    }

    private fun drawPaintStyles(canvas: Canvas) {
        // STROKE 样式
        paint.strokeWidth = 10f
        paint.style = Paint.Style.STROKE
        paint.color = Color.RED
        canvas.drawCircle(100f, 100f, 50f, paint)

        // FILL 样式
        paint.style = Paint.Style.FILL
        paint.color = Color.GREEN
        canvas.drawCircle(250f, 100f, 50f, paint)

        // FILL_AND_STROKE 样式
        paint.style = Paint.Style.FILL_AND_STROKE
        paint.color = Color.BLUE
        canvas.drawCircle(400f, 100f, 50f, paint)
    }

    private fun drawGradients(canvas: Canvas) {
        // 线性渐变
        val linearShader = LinearGradient(
            0f, 0f, 200f, 200f,
            Color.RED, Color.BLUE,
            Shader.TileMode.CLAMP
        )
        paint.shader = linearShader
        canvas.drawRect(50f, 50f, 250f, 250f, paint)

        // 径向渐变
        val radialShader = RadialGradient(
            350f, 150f, 100f,
            Color.YELLOW, Color.GREEN,
            Shader.TileMode.CLAMP
        )
        paint.shader = radialShader
        canvas.drawCircle(350f, 150f, 100f, paint)

        // 扫描渐变
        val sweepShader = SweepGradient(
            200f, 400f,
            intArrayOf(Color.RED, Color.GREEN, Color.BLUE, Color.RED),
            null
        )
        paint.shader = sweepShader
        canvas.drawCircle(200f, 400f, 100f, paint)
    }

    private fun drawPaths(canvas: Canvas) {
        // 简单路径
        path.reset()
        path.moveTo(50f, 50f)
        path.lineTo(150f, 50f)
        path.lineTo(100f, 150f)
        path.close()
        paint.color = Color.RED
        paint.style = Paint.Style.FILL
        canvas.drawPath(path, paint)

        // 弧形路径
        path.reset()
        val rectF = RectF(200f, 50f, 300f, 150f)
        path.addArc(rectF, 0f, 270f)
        paint.color = Color.BLUE
        paint.style = Paint.Style.STROKE
        paint.strokeWidth = 5f
        canvas.drawPath(path, paint)

        // 贝塞尔曲线
        path.reset()
        path.moveTo(50f, 200f)
        path.quadTo(100f, 100f, 150f, 200f)
        paint.color = Color.GREEN
        paint.style = Paint.Style.STROKE
        paint.strokeWidth = 5f
        canvas.drawPath(path, paint)

        // 三次贝塞尔曲线
        path.reset()
        path.moveTo(200f, 200f)
        path.cubicTo(250f, 100f, 300f, 300f, 350f, 200f)
        paint.color = Color.MAGENTA
        canvas.drawPath(path, paint)
    }

    private fun drawTextStyles(canvas: Canvas) {
        // 基本文本
        paint.color = Color.BLACK
        paint.textSize = 40f
        paint.style = Paint.Style.FILL
        canvas.drawText("普通文本", 50f, 100f, paint)

        // 加粗文本
        paint.isFakeBoldText = true
        canvas.drawText("加粗文本", 50f, 150f, paint)
        paint.isFakeBoldText = false

        // 斜体文本
        paint.textSkewX = -0.25f
        canvas.drawText("斜体文本", 50f, 200f, paint)
        paint.textSkewX = 0f

        // 下划线文本
        paint.isUnderlineText = true
        canvas.drawText("下划线文本", 50f, 250f, paint)
        paint.isUnderlineText = false

        // 删除线文本
        paint.isStrikeThruText = true
        canvas.drawText("删除线文本", 50f, 300f, paint)
        paint.isStrikeThruText = false

        // 大号文本
        paint.textSize = 60f
        paint.color = Color.RED
        canvas.drawText("大号文本", 50f, 370f, paint)
        paint.textSize = 40f
    }

    fun reset() {
        styleIndex = (styleIndex + 1) % 5
        invalidate()
    }
}